package net.gz01.wallet.web.controller.admin;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.TopBank;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.Contacts;
import net.gz01.wallet.web.model.Merch;

public class ContactsController extends BaseController {
	public void search() {
		setAttr("topBankList", TopBank.topBankList);
		render("merch/contactsList.html");
	}

	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<Contacts> pageResult = Contacts.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void addBankCardPage() {
		setAttr("merchList", Merch.dao.selectToRedis());
		setAttr("topBankList", TopBank.topBankList);
		render("merch/contactsBankCardAdd.html");
	}

	public void addWalletPage() {
		setAttr("merchList", Merch.dao.selectToRedis());
		setAttr("topBankList", TopBank.topBankList);
		render("merch/contactsWalletAdd.html");
	}

	public void updatePage() {
		Contacts contacts = Contacts.dao.findById(getPara("id"));
		setAttr("contacts", contacts);
		setAttr("topBankList", TopBank.topBankList);
		setAttr("merchList", Merch.dao.selectToRedis());
		render("merch/contactsUpdate.html");
	}

	@Before(Tx.class)
	public void add() {
		Contacts contacts = getModel(Contacts.class);
		boolean exists = Contacts.dao.exists(contacts.getMerchCode(), contacts.getAccountType(), contacts.getAccountNo());
		if (exists) {
			renderFail("该收款账户已存在");
			return;
		}

		Merch myMerch = Merch.dao.selectByMerchCode(contacts.getMerchCode());
		contacts.setMerchName(myMerch.getName());

		if ("bankCard".equals(contacts.getAccountType())) {
			Merch merch = Merch.dao.selectByMerchCode(contacts.getMerchCode());
			if (StrKit.isBlank(contacts.getBankEnglishLetters())) {
				renderFail("请选择银行");
				return;
			}
//			if (merch.getName().equals(contacts.getAccountName())) {
//				contacts.setIsSelf(true);
//			} else {
//				contacts.setIsSelf(false);
//			}
			contacts.setBankName(TopBank.getByShortEnName(contacts.getBankEnglishLetters()).getBankName());
		}
		if ("wallet".equals(contacts.getAccountType())) {
			Merch merch = Merch.dao.selectByMerchCode(contacts.getAccountNo());
			contacts.setAccountName(merch.getName());
			contacts.setIsSelf(false);
		}
		contacts.save();
		renderSuccess();
	}

	@Before(Tx.class)
	public void update() {
		Contacts contacts = getModel(Contacts.class);
		Merch myMerch = Merch.dao.selectByMerchCode(contacts.getMerchCode());
		contacts.setMerchName(myMerch.getName());
		if ("bankCard".equals(contacts.getAccountType())) {
			Merch m = Merch.dao.selectByMerchCode(contacts.getMerchCode());
			contacts.setBankName(TopBank.getByShortEnName(contacts.getBankEnglishLetters()).getBankName());
//			if (m.getName().equals(contacts.getAccountName())) {
//				contacts.setIsSelf(true);
//			} else {
//				contacts.setIsSelf(false);
//			}
		}
		contacts.update();
		renderSuccess();
	}

	@Before(Tx.class)
	public void delete() {
		Contacts.dao.deleteById(getPara("id"));
		renderSuccess(false);
	}
}
